// pages/recruit/application.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      nameOfAppl:'',
      schoolOfAppl:'',
      stunumOfAppl:'',
      departmentOfAppl:'',
      majorOfAppl:'',
      gradeOfAppl:'',
      mobileOfAppl:'',
      weixinOfAppl:'',
      resumeOfAppl:'',
      attachmentOfAppl:'',
      leaderOfPrg:'',
      collegeOfPrg:'',
      nameOfPrg:'',
      nicknameOfAppl:'',
      portraitOfAppl:'',
      qqOfAppl:'',
      sessionID:'',
      chuangNum:'',
    },
    
    department:function(e){
      let val=e.detail.value;
      this.setData({
        departmentOfAppl:val
      })
    },

    major:function(e){
      let val=e.detail.value;
      this.setData({
        majorOfAppl:val
      })
    },

    grade:function(e){
      let val=e.detail.value;
      this.setData({
        gradeOfAppl:val
      })
    },

    mobile:function(e){
      let val=e.detail.value;
      this.setData({
        mobileOfAppl:val
      })
    },

    weixin:function(e){
      let val=e.detail.value;
      this.setData({
        weixinOfAppl:val
      })
    },

    resume:function(e){
      let val=e.detail.value;
      this.setData({
        resume:val
      })
    },

    gotoApplication(e) {
      wx.request({
        url: 'https://www.hschuangsai.com/user/update',
        data: {
          attachment:this.data.attachmentOfAppl,
          department:this.data.departmentOfAppl,
          grade:this.data.gradeOfAppl,
          major:this.data.majorOfAppl,
          nickname:this.data.nicknameOfAppl,
          portrait:this.data.portraitOfAppl,
          qq:this.data.qqOfAppl,
          resume: this.data.resume,
          telephone:this.data.mobileOfAppl,
          wechatNum:this.data.weixinOfAppl,
        },
        method: 'POST',
        header: {
            'content-type': 'application/json',
            'session': this.data.sessionID
        },
        success: (res) => {
          if (res.statusCode!=200) {
            wx.showToast({
                title: '简历更新失败，请重试',
                icon: 'none',
                duration: 800     
              })  
        }
            console.log('简历更新成功',res);
            if (res.data.code==-1){
              wx.showToast({
                icon:'none',
                title: '请先进入“我的”并登录！',
              })
            }
              wx.navigateTo({
                url: '/pages/recruit/application/application?idOfPrg='+this.data.idOfPrg,
              })  
            
        },
        fail: (err) => {
            console.log('登录过期了', err);
              wx.showToast({
                title: '请先进入“我的”并登录！',
                icon: 'none',
                duration:2000
              })
        }
    })
        },
    
    gotoDescriptEditor()
    {
      wx.navigateTo({
        url: '/pages/index/index?page=1',
      })  
    },
       
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.setData({
        nameOfAppl:'',
        schoolOfAppl:'',
        stunumOfAppl:'',
        departmentOfAppl:'',
        majorOfAppl:'',
        gradeOfAppl:'',
        mobileOfAppl:'',
        weixinOfAppl:'',
        resumeOfAppl:'',
        leaderOfPrg:'',
        collegeOfPrg:'',
        nameOfPrg:'',
        loginFlag:0,
        idOfPrg:options.idOfPrg,
        chuangNum: wx.getStorageSync('chuangNum')
      })
      this.setData({
        userInfo: wx.getStorageSync('userInfo'), // 用户的微信头像、昵称等信息
        loginFlag: wx.getStorageSync('loginFlag'), // 判断是否登录，登录为1，未登录为0或空
        sessionID: wx.getStorageSync('sessionID'),
        chuangNum: wx.getStorageSync('chuangNum'),
      })
    if (this.data.loginFlag != 1) {
        this.myShowModal();
    }
    wx.request({
      url: 'https://www.hschuangsai.com/user/info?userChuangNum='+this.data.chuangNum,
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'session': this.data.sessionID
    },
      // 请求登录态成功
      success: (res) => {
        if (res.statusCode!=200) {
          wx.showToast({
              title: '获取用户信息失败，请重试',
              icon: 'none',
              duration: 800     
            })  
      }
          console.log('获取用户信息', res)
          this.setData({
            nameOfAppl:res.data.data.name,
            schoolOfAppl:res.data.data.school,
            stunumOfAppl:res.data.data.studentNumber,
            departmentOfAppl:res.data.data.department,
            majorOfAppl:res.data.data.major,
            gradeOfAppl:res.data.data.grade,
            mobileOfAppl:res.data.data.telephone,
            weixinOfAppl:res.data.data.wechatNum,
            resume:res.data.data.resume,
          })
          this.setData({
            str:wx.getStorageSync('DescriptHtml')
       })
        console.log("str",this.data.str)
        if(this.data.str==""&&this.data.resume!="")
        {
            console.log("resume load")
            var strr=JSON.stringify(this.data.resume)
            wx.setStorageSync('DescriptHtml',strr)
        }
  },
})
          wx.request({
            url:'https://www.hschuangsai.com/recruitProject/info?projectId='+this.data.idOfPrg,
            method:"GET",
            success:(res2)=>{
              console.log(res2.data)
              this.setData({
                leaderOfPrg:res2.data.data.recruitProject.captainName,
                collegeOfPrg:res2.data.data.recruitProject.institute,
                nameOfPrg:res2.data.data.recruitProject.name,
              })
            }
          })
    },
    
   myShowModal() {
    wx.showModal({
        title: '提示',
        content: '登录后查看更多，是否登录？',
        success: (res) => {
            // 确定
            if (res.confirm) {
                this.logIn();
            }
            else {
                 wx.showToast({
                     title: '跳转到首页',
                     icon: 'none',
                    duration: 1500
                 })
                 setTimeout(function () {
                  wx.switchTab({
                    url: '/pages/recruit/recruit',
                    success:function(e)
                    {
                      var page = getCurrentPages().pop();
                      if (page == undefined || page == null) return;
                      page.onLoad();
                    }
                  })  
                }, 1500)
            }
        }
    })
  },

  logIn() {
    // 如果userInfo为空，则未确认过微信授权弹窗，即首次登录
    if (!this.data.userInfo) {
        // 微信授权弹窗
        wx.getUserProfile({
            desc: '用于获取用户头像和昵称',
            // 用户点击确认
            success: (res) => {
                let userInfo = res.userInfo;
                wx.setStorageSync('userInfo', userInfo); // 缓存userInfo
                this.myLogin(); // 进行登录
            },
            // 用户点击取消
            fail: (res) => {
                // wx.showToast({
                //     title: '跳转到首页',
                //     icon: 'none',
                //     duration: 1500
                // })
            }
        })
    }
    // 非首次登录
    else {
        this.myLogin(); // 重新登录
    }
},

// wx.login()封装
myLogin() {
    wx.login({
        success: (res) => {
            wx.request({
                url: 'https://www.hschuangsai.com/user/login/' + res.code,
                method: 'GET',
                success: (res) => {
                    console.log("myLogin success", res);
                    let sessionID = res.data.data.sessionId;
                    wx.setStorageSync('chuangNum',res.data.data.chuangNum)
                    wx.setStorageSync('sessionID', sessionID); // 缓存sessionID
                    wx.setStorageSync('loginFlag', 1); // 缓存loginFlag
                    this.setData({
                        sessionID: sessionID,
                        loginFlag: 1,
                    });

                    wx.showToast({
                        title: '成功登录，继续创建队伍',
                        icon: 'none',
                        duration: 2000
                    })
                }
            })
        }
    })
},



    

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow(){
      this.setData({
        html:JSON.parse(wx.getStorageSync('DescriptHtml'))
   })
   var WxParse = require('../../wxParse/wxParse.js');
   var data=this.data;
   WxParse.wxParse('content', 'html', data.html, this, 10);
  },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})